package oop.ex1.filters;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * a class that gets a file and "decides" whether the file stand in the user Requirements
 * all the filter class inherits  from this file
 * @author daniel shalev
 * @author dan almog
 */
public class Filters implements FileFilter{
	//the filters
	private ArrayList<Filters[]> filters;
	
	public Filters() {}
	
	/**
	 * construct a new filter object and saves the filter list localy
	 * @param filters the filters to save
	 */
	public Filters(ArrayList<Filters[]> filters){
		this.filters=filters;
	}
	
	/**
	 * checks the files for more then one filter in a line(uses OR between the filters)
	 * @param pathname the file to check
	 * @param filters the array of filters to chack
	 * @return true if the file stand's up to any of the user requirements and false otherwie
	 */
	private boolean checkOr(File pathname, Filters[] filters){
		for(int i=0; i<filters.length;i++){
			//runs on all of the or filters of one line
			if(filters[i].accept(pathname))
				return true;
		}
		return false;
	}

	/**
	 * a method that check's if the file stand's up to all the user requirements
	 * @param pathname the file to check
	 * @return true if the file passed all the filters' false otherwise
	 */
	@Override
	public boolean accept(File pathname){
		Iterator<Filters[]> iter=filters.iterator();
		Filters[] next;
		//runs on all the filter listArray
		while(iter.hasNext()){
			next=iter.next();
			//check if there is more then one filter in the line
			if(next.length>1){
				if(!checkOr(pathname,next)){
					return false;
				}
			}	
			else{
				if(!next[0].accept(pathname))
					return false;
			}
		}
		return true;
	}
	
	
	
	
}	
